package com.hht.study.juc;

import java.util.Random;

/**
 * @author hht
 * @date 2022/3/15
 */
public class ThreadLocalTest {

    public static void main(String[] args) throws InterruptedException {

        for(int i=0;i<10;i++){
            task task = new task();
            Thread thread = new Thread(task);
            Thread.sleep(new Random().nextInt(1000));
            thread.start();
        }
    }

    static class task implements Runnable{

        //ThreadLocal<Integer> threadLocal=ThreadLocal.withInitial(()-> 1);
        private static ThreadLocal<String> threadLocal=ThreadLocal.withInitial(()->"asd");
        @Override
        public void run() {
            System.out.println("当前线程--"+Thread.currentThread().getName()+",默认值为"+threadLocal.get());
            try {
                Thread.sleep(new Random().nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            threadLocal.set("qqqq");
            System.out.println("当前线程--"+Thread.currentThread().getName()+",修改值后为"+threadLocal.get());
        }
    }
}
